<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission({type: 1, pid: '0'})">添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPermission({type: 1, pid: row.id})">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible="showDialog" title="弹层标题" @close="colesDialog">
      <el-form ref="addForm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermissionAPI, detailPermissionAPI, updatePermission, delPeermissionAPI } from '@/api/Permission'
import transTree from '@/utils/transTree'
export default {
  name: 'PermissionIndex',
  data() {
    const validateCode = (rule, value, callback) => {
      // 拓展思考：
      // 1. 要求不能和同级出现重复 -> 要在生成的codeList基础上再做一层过滤 以type作为过滤条件
      // 2. 编辑状态下容许和自己重复 -> 根据当前form.id是否存在 对codeList做处理 如果编辑 就把自己先删除掉
      let codeList = []
      // 判断是否有id，如果有则是编辑状态，没有是新增状态
      if (this.codeList.id) {
        // 把和当前编辑项的code不相等的code重新封装到新数组中
        codeList = this.codeList.map(item => item.code).filter(code => code !== this.cacheCode)
      } else {
        codeList = this.codeList.map(item => item.code)
      }
      // 判断输入的code和接口返回的所有code是否重复
      if (codeList.includes(value)) {
        callback(new Error('标识不能重复'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      codeList: [],
      cacheCode: null,
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: null, // 添加的权限类型 如果是1级 type=1 如果2级 type = 2
        pid: '', // 如果点击的上面的 传固定的0 如果点击表格内 要以当前行的id为主
        enVisible: ''
      },
      rules: {
        code: [{ validator: validateCode, trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getPermissionList()
      this.list = transTree(res)
      this.codeList = res
    },
    colesDialog() {
      this.showDialog = false
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: '',
        enVisible: ''
      }
    },
    addPermission({ type, pid }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async confirm() {
      if (this.formData.id) {
        // 编辑弹框
        await updatePermission(this.formData)
      } else {
        // 新增
        await addPermissionAPI(this.formData)
      }
      this.getList()
      this.showDialog = false
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: '',
        enVisible: ''
      }
    },
    async editPermission(id) {
      const res = await detailPermissionAPI(id)
      this.formData = res
      this.cacheCode = res.code
      this.showDialog = true
    },
    async delPermission(id) {
      this.$confirm('确定要删除此角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPeermissionAPI(id)
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {

      })
    }
  }
}
</script>
